package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.os.Looper;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.ArrayList;
import java.util.List;

import liuliu.custom.control.toolbar.TToolbar;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.model.CodeModel;

/**
 * Created by Administrator on 2015/12/7.
 */
public class SearchAreaListActivity extends BaseActivity {
    @CodeNote(id = R.id.area_name)
    EditText nametext;
    @CodeNote(id = R.id.area_lv)
    ListView lv;
    List<CodeModel> list;
    List<CodeModel> myList;
    @CodeNote(id = R.id.area_search_toolbar)
    TToolbar toolbar;

    @Override
    public void initViews() {
        setContentView(R.layout.serach_area_list);
        myList = new ArrayList<CodeModel>();
        list = finalDb.findAllByWhere(CodeModel.class, " codeName='Code_Native'");
        myList = list;
        CommonAdapter adapter = new CommonAdapter<CodeModel>(SearchAreaListActivity.this, list, R.layout.right_menu_item) {
            @Override
            public void convert(ViewHolder holder, CodeModel o, int position) {
                holder.setText(R.id.right_menu_title, o.getValue());
            }
        };
        lv.setAdapter(adapter);
    }

    @Override
    public void initEvents() {
        toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                Intent intent = new Intent();

                setResult(120, intent);
                finish();
            }
        });
        nametext.addTextChangedListener(textWatcher);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CodeModel code = myList.get(position);
                Intent intent = new Intent();
                intent.putExtra("CodeAreaName", code.getName());
                intent.putExtra("CodeAreaValue", code.getValue());
                setResult(12, intent);
                finish();
            }
        });
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            System.out.println("infoafter");
            System.out.println("infoafterchange");

            myList = new ArrayList<CodeModel>();
            for (int i = 0; i < list.size(); i++) {
                int index = list.get(i).getValue().indexOf(nametext.getText().toString());
                if (index >= 0) {
                    myList.add(list.get(i));
                }
            }
            CommonAdapter adapter = new CommonAdapter<CodeModel>(SearchAreaListActivity.this, myList, R.layout.right_menu_item) {
                @Override
                public void convert(ViewHolder holder, CodeModel o, int position) {
                    holder.setText(R.id.right_menu_title, o.getValue());
                }
            };
            lv.setAdapter(adapter);
        }

    };

}
